import { _decorator, Component, director} from 'cc';
import { IGameSystem } from '../Interfaces/IGameSystem';
const { ccclass, property } = _decorator;

@ccclass("EventSystem")
export class EventSystem extends Component
{
    public static instance : EventSystem = null;
    public static get Instance(): EventSystem
    {
        return this.instance;
    }

    onLoad(){
        EventSystem.instance = this;
        director.addPersistRootNode(this.node);
    }

    
    private updateEventList = new Array<IGameSystem.IUpdateSystem>();
    public RegeisterUpdateEvent(event : IGameSystem.IUpdateSystem)
    {
        var index = this.updateEventList.indexOf(event);
        if(index == -1)
        {
            this.updateEventList.push(event);
        }
        else{
            console.log("__RegeisterUpdateEvent__can not ! " + index)
            console.log(event)
        }
    }

    public ResigerAwakeEventList(event : IGameSystem.IAwakeSystem)
    {
        event.Awake();
    }

   update(deltaTime : number)
   {
        this.updateEventList.forEach((event)=>{
            event.Update();
        })
   }
}